/**
 * @Author      : maolin
 * @Date        : 2021/3/17
 * @Version     : 1.0
 * @Description : 处理数据业务逻辑
 **/
import {GET_ROLE_LIST} from "./api";

//获取角色列表
export async function getRoleTree (node) {
  if (node && node.data && node.data.id) {
    return [];
  }
  let data = (await GET_ROLE_LIST({pageIndex: 1, pageSize: 1000, status: 10})).data.list;
  let array = [];
  if (data) {
    array = data.map((item) => {
      let newItem = item;
      newItem.nodeId = item.id;
      newItem.roleId = item.id;
      return newItem;
    });
  }
  return array;
}
export function getItemFromTreeById(id, tree, valueKey) {
  let item = null;
  return _rec(id, tree, valueKey);
  function _rec(id, treeData, valueKey) {
    if (valueKey === undefined) {
      valueKey = 'id';
    }
    if (treeData && treeData.length > 0) {
      for (let i in treeData) {
        if (treeData[i] !== undefined && treeData[i][valueKey] == id) {
          item = treeData[i] || null;
          break;
        }
        if (treeData[i].childs && treeData[i].childs.length > 0) {
          _rec(id, treeData[i].childs, valueKey);
        }
      }
    }
    return item;
  }
}
